var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var session = require('express-session')
var indexRouter = require('./routes/index');
var userRouter = require('./routes/userRouter');
var managerRouter = require('./routes/managerRouter');
var pubRouter = require('./routes/pubRouter');


var app = express();

/*let busboy = require('connect-busboy');
app.use(busboy());*/
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
``
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
/*session引入*/
/*session引入*/

app.use(cookieParser('session'));
process.env.PORT = 3001;
app.set('port', process.env.PORT || 3001)
app.use(session({
    secret: 'sessionid-task', // 建议统一命名，尽量复杂  同一个名字，可以实现session共享
    name: 'sessionid-task', // 建议统一命名，尽量复杂，同一个名字，可以实现session共享
    saveUninitialized: false,
    resave: false,
    unset: 'destroy',
    maxAge: 24 * 60 * 60 * 1000,
    rolling:true,

    cookie: { maxAge: 24 * 60 * 60 * 1000 },
    /*    store: new RedisStore({
            client: redisClient,
            ttl: 24 * 60 * 60,
            prefix: 'node-session'
        })*/
}));




//app.use(express.static("./node_modules"));
app.use(express.static(path.join(__dirname, '/')));

/*各种router和路径捆绑起来*/
app.use('/', indexRouter);
app.use('/user', userRouter);
app.use('/manager', managerRouter);
app.use('/pub', pubRouter);




// catch 404 and forward to error handler
app.use(function (req, res, next) {
    next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error');

});




module.exports = app
